package com.xysq.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rockcent.action.AppAction;
import com.rockcent.model.CustomerCouponBO;
import com.rockcent.model.constant.FavoriteState;
import com.umeng.analytics.MobclickAgent;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.CircleImageView;

import butterknife.InjectView;

/**
 * 描述信息
 *
 * @author keegan
 * @date 15/8/5
 */
public class WantBuyItemAdapter extends BaseDataAdapter<CustomerCouponBO> {

    private Context context;
    private AppAction appAction;

    public WantBuyItemAdapter(Context context, AppAction appAction) {
        super(context);
        this.context = context;
        this.appAction = appAction;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_sgird, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final CustomerCouponBO customerCouponBO = itemList.get(position);
//        imageLoader.displayImage(PropertiesUtil.getImageServer() + customerCouponBO.getLogoUrl(), holder.goodsImg);
        if (TextUtils.isEmpty(customerCouponBO.getLogoUrl()) || customerCouponBO.getLogoUrl().equals("null")) {
            holder.goodsImg.setImageResource(R.drawable.def_img);
        } else {
            if (customerCouponBO.getLogoUrl().startsWith("http://")) {
                imageLoader.displayImage(customerCouponBO.getLogoUrl(), holder.goodsImg);
            } else {
                imageLoader.displayImage(PropertiesUtil.getImageServer() + customerCouponBO.getLogoUrl(), holder.goodsImg);
            }
        }
        holder.titleTxt.setText(customerCouponBO.getName());
        holder.priceTxt.setText("￥" + UseUtil.handlePrice(customerCouponBO.getPrice()));
//        String name = customerCouponBO.getNickName();
//        if (TextUtils.isEmpty(name)) {
//            name = customerCouponBO.getCustomerName();
//        }
//        if (TextUtils.isEmpty(name)) {
//            name = UseUtil.handleWithStar(customerCouponBO.getPhone());
//        }
        String name = UseUtil.selectName(customerCouponBO.getNickName(), customerCouponBO.getCustomerName(), customerCouponBO.getPhone());
        holder.userNameTxt.setText(name);
        if (TextUtils.isEmpty(customerCouponBO.getHeadUrl()) || customerCouponBO.getHeadUrl().equals("null")) {
            holder.userCimg.setImageResource(R.drawable.def_img_head);
        } else {
            if (customerCouponBO.getHeadUrl().startsWith("http://")) {
                imageLoader.displayImage(customerCouponBO.getHeadUrl(), holder.userCimg);
            } else {
                imageLoader.displayImage(PropertiesUtil.getImageServer() + customerCouponBO.getHeadUrl(), holder.userCimg);
            }
        }
        if (customerCouponBO.getIsFavorite() == FavoriteState.YES) {
            holder.loveImg.setImageResource(R.drawable.ic_love_solid_big);
        } else {
            holder.loveImg.setImageResource(R.drawable.ic_love_hollow_big);
        }
        holder.loveImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(context, "1006");
                String phoneNumber = UserInfoKeeper.readPhoneNumber(context);
                if (TextUtils.isEmpty(phoneNumber)) {
                    ToastUtil.showShort(context, "请先登录");
                    return;
                }
                if(customerCouponBO.getIsFavorite() == FavoriteState.NO) {
                    customerCouponBO.setIsFavorite(FavoriteState.YES);
                    notifyDataSetChanged();
                    appAction.addFavorite(phoneNumber, customerCouponBO.getId(), null);
                }else {
                    ToastUtil.showShort(context, "您已经喜欢过该商品");
                }
            }
        });

        return convertView;
    }

    static class ViewHolder extends BaseViewHolder {
        @InjectView(R.id.img_goods)
        ImageView goodsImg;
        @InjectView(R.id.txt_title)
        TextView titleTxt;
        @InjectView(R.id.txt_price)
        TextView priceTxt;
        @InjectView(R.id.img_love)
        ImageView loveImg;
        @InjectView(R.id.cimg_user)
        CircleImageView userCimg;
        @InjectView(R.id.txt_user_name)
        TextView userNameTxt;

        public ViewHolder(View view) {
            super(view);
        }
    }
}
